package csc.fresher.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import csc.fresher.domain.User;

public class UserDAOImpl implements UserDAO {
	
	public static List<User> listUser = new ArrayList<User>();
	
	public UserDAOImpl() {
		
	}

	@Override
	public User getUser(String username) {
		User u = new User();
		for (int i = 0; i < listUser.size(); i++) {
			if(listUser.get(i).getUsername().equals(username)){
				return listUser.get(i);
			}
		}
		return u;
	}

	@Override
	public List<User> getListUser() {
		return listUser;
	}

	@Override
	public boolean addUser(User user) {
		listUser.add(user);
		return true;
	}

	@Override
	public boolean deleteUser(String username) {
		
		for (int i = 0; i < listUser.size(); i++) {
			User u = listUser.get(i);
			if(u.getUsername().equals(username)){
				listUser.remove(u);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean editUser(String username, String newpassword) {
		User uNew = new User(username, newpassword, false);
		for (int i = 0; i < listUser.size(); i++) {
			User u = listUser.get(i);
			if(u.getUsername().equals(username)){
				uNew.setRegisterDate(u.getRegisterDate());
				uNew.setModifiedDate(u.getModifiedDate());
				uNew.setUploadDate(u.getUploadDate());
				uNew.setDownloadDate(u.getDownloadDate());
				uNew.setCapacity(u.getCapacity());
				listUser.remove(u);
				listUser.add(uNew);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean checkUser(String username) {
		User u = new User();
		for (int i = 0; i < listUser.size(); i++) {
			if(listUser.get(i).getUsername().equals(username)){
				return true;
			}
		}
		return false;
	}

}
